const R = require('ramda')
//dcfm.eastmoney.com返回的jsonp字符串一般是如下格式：
//返回的格式"jQuery1123048543535627263656_1621419393782({...})"
//需要提取json字符串，并解析。
const parseJsonp = txt => {
    let first = txt.indexOf('(')
    let last = txt.lastIndexOf(')')
    let content = txt.substring(first + 1, last)
    let obj = {}
    try {
        obj = JSON.parse(content)
    } catch(err) {
        console.log(err)
    }
    return obj
}

const keysToLower = R.pipe(
    R.toPairs,
    R.map(R.adjust(0, R.toLower)),
    R.fromPairs
)

const toFloat = str => {
    let result = parseFloat(str)
    return isNaN(result) ? null : result
}

const norm = (mean, std) => {
    const u1 = Math.random()
    const u2 = Math.random()
    const z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2.0 * Math.PI * u2)
    return z0 * std + mean
}

module.exports = {
    parseJsonp, keysToLower, toFloat, norm
}
